package com.qf.listener;

import com.event.util.EventHandler;
import com.event.util.EventType;
import com.qf.entity.Goods;
import com.qf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 下单消息的处理
 */
@Component
@EventType("orders_msg")
public class OrdersMsgHandler implements EventHandler<Map> {

    @Autowired
    private GoodsService goodsService;

    @Override
    public void msgHandler(Map map) {
        System.out.println("商品服务接收到下单的消息：" + map);
        //获得商品信息
        Integer gid = (Integer) map.get("gid");
        //扣减商品的库存
        Goods goods = goodsService.getById(gid);
        goods.setSave(goods.getSave() - 1);
        goodsService.updateById(goods);
    }
}
